package com.yjdb.sanyuantang.modules.Admin.service.Impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.yjdb.sanyuantang.common.entity.SysAdmin;
import com.yjdb.sanyuantang.common.entity.SysAdminRelationRole;
import com.yjdb.sanyuantang.common.mapper.SysAdminMapper;
import com.yjdb.sanyuantang.common.mapper.SysAdminRelationRoleMapper;
import com.yjdb.sanyuantang.modules.Admin.mapper.AdminUserMapper;
import com.yjdb.sanyuantang.modules.Admin.service.AdminUserService;
import com.yjdb.sanyuantang.modules.Admin.DTO.admin.AdminAddDTO;
import com.yjdb.sanyuantang.modules.Admin.DTO.admin.AdminUpdateDTO;
import com.yjdb.sanyuantang.modules.Admin.DTO.admin.AdminPage;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * packageName com.yjdb.sanyuantang.modules.Admin.service.Impl
 *
 * @author 混沌酱
 * @version 1.0
 * @className AdminUserServiceImpl
 * @date 2024/3/14
 * @description TODO
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private SysAdminMapper sysAdminMapper;

    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private SysAdminRelationRoleMapper sysAdminRelationRoleMapper;


    /**
     * 添加用户
     *
     * @param adminAddDTO
     */
    @Override
    public void add(AdminAddDTO adminAddDTO) {
        // 查询用户名是否存在
        Map<String, Object> filer = new HashMap<>();
        filer.put("username", adminAddDTO.getUsername());
        List<SysAdmin> list = sysAdminMapper.selectByMap(filer);
        if (adminAddDTO.getUsername().equals("") && adminAddDTO.getUsername() != null) {
            throw new RuntimeException("用户名不能为空");
        }
        if (!list.isEmpty()) {
            throw new RuntimeException("用户名已存在");
        }
        // 添加用户
        SysAdmin sysAdmin = new SysAdmin();
        BeanUtils.copyProperties(adminAddDTO, sysAdmin);
        sysAdmin.setCreateTime(new Date());
        sysAdmin.setUpdataTime(new Date());
        sysAdmin.setIsDelete(0);
        sysAdmin.setIsStatus("1");
        sysAdmin.setPassword(SaSecureUtil.sha256(sysAdmin.getPassword()));
        sysAdminMapper.insert(sysAdmin);
    }


    /**
     * 修改用户
     *
     * @param adminUpdateDTO
     */
    @Override
    public void updata(AdminUpdateDTO adminUpdateDTO) {
        // 查询用户名是否存在
        adminUpdateDTO.setUsername(null);
        // 修改用户
        SysAdmin sysAdmin = new SysAdmin();
        BeanUtils.copyProperties(adminUpdateDTO, sysAdmin);
        sysAdmin.setUpdataTime(new Date());
        sysAdmin.setUsername(null);
        sysAdmin.setPassword(SaSecureUtil.sha256(sysAdmin.getPassword()));
        sysAdminMapper.updateById(sysAdmin);
    }


    /**
     * 删除用户
     *
     * @param id
     * @param username
     */
    @Override
    public void delete(Integer id, String username) {
        // 删除用户
        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setId(id.longValue());
        sysAdmin.setIsDelete(1);
        sysAdmin.setUpdataTime(new Date());
        sysAdminMapper.updateById(sysAdmin);
    }

    /**
     * 启用或关闭用户
     *
     * @param id
     * @param username
     * @param enable
     */
    @Override
    public void enable(Integer id, String username, String enable) {
        // 启用或关闭用户
        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setId(id.longValue());
        sysAdmin.setIsStatus(enable);
        sysAdmin.setUpdataTime(new Date());
        sysAdminMapper.updateById(sysAdmin);
    }

    /**
     * 查询用户列表 分页查询
     *
     * @param adminPage
     * @return
     */
    @Override
    public List<AdminUpdateDTO> list(AdminPage adminPage) {
        if (adminPage.getEndTime()==null || adminPage.getEndTime().isEmpty()){
            adminPage.setEndTime("9999-12-31");
        }
        if (adminPage.getStartTime()==null || adminPage.getStartTime().isEmpty()){
            adminPage.setStartTime("1970-01-01");
        }
        List<AdminUpdateDTO> list = adminUserMapper.list(adminPage);
        return list;
    }

    /**
     * 绑定角色
     *
     * @param id
     * @param roleIds
     */
    @Override
    public void bindRole(Integer id, String roleIds) {
        // 角色用户绑定关系
        SysAdminRelationRole sysAdminRelationRole = new SysAdminRelationRole();
        sysAdminRelationRole.setAdminId(id.longValue());
        sysAdminRelationRole.setUpdataTime(new Date());
        sysAdminRelationRole.setRoleId(roleIds);
        // 查询用户是否有角色
        Map<String, Object> filer = new HashMap<>();
        filer.put("admin_id", id);
        List<SysAdminRelationRole> list = sysAdminRelationRoleMapper.selectByMap(filer);
        if (!list.isEmpty()){
            sysAdminRelationRole.setId(list.get(0).getId());
            sysAdminRelationRoleMapper.updateById(sysAdminRelationRole);
        }else {
            sysAdminRelationRole.setCreateTime(new Date());
            sysAdminRelationRoleMapper.insert(sysAdminRelationRole);
        }
    }
}
